package com.titaniume.rabbitmq.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: rabbitmq
 * @Package: com.titaniume.rabbitmq.springbootrabbitmq.config
 * @ClassName: ConfirmConfig
 * @Author: titan
 * @Description:
 * @Date: 2022/1/3 16:06
 * @Version: 1.0
 */
@Configuration
public class ConfirmConfig {
    public static final String CONFIRM_EXCHANGE_NAME ="confirm.exchange";
    public static final String CONFIRM_QUEUE_NAME ="queue.exchange";
    public static final String CONFIRM_ROUTING_KEY ="key1";

    //备份交换机
    public static final String BACKUP_EXCHANGE_NAME ="backup.exchange";
    //备份队列
    public static final String BACKUP_QUEUE_NAME ="back.queue";
    //警告队列
    public static final String WARNING_QUEUE_NAME  ="warning.queue";




    @Bean("confirmeExchange")
    public DirectExchange confirmeExchange(){

        return  ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME).durable(true).withArgument("alternate-exchange",BACKUP_EXCHANGE_NAME).build();
    }


    @Bean("confirmeQueue")
    public Queue confirmeQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }


    //声明确认队列绑定关系
    @Bean
    public Binding queueBinding(@Qualifier("confirmeQueue") Queue queue,
                                @Qualifier("confirmeExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY);
    }

    //备份交换机
    @Bean("backupExchange")
    public FanoutExchange backupExchange(){
        return  new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    //备份队列
    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    //警告队列
    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }

    @Bean
    public  Binding backupQueueBindingBackupExchange(@Qualifier("backupQueue") Queue queue,
                                                     @Qualifier("backupExchange") FanoutExchange backupExchange)
    {
        return BindingBuilder.bind(queue).to(backupExchange);

    }

    @Bean
    public  Binding warningQueueBindingBackupExchange(@Qualifier("warningQueue") Queue queue,
                                                      @Qualifier("backupExchange") FanoutExchange backupExchange)
    {
        return BindingBuilder.bind(queue).to(backupExchange);

    }


}
